/**
 * 部门管理模块
 * 负责管理部门信息，包括部门列表、员工数量统计和部门主管信息
 */

// 定义状态
const state = {
  // 部门列表，包含初始数据
  list: [
    { id: 1, name: '技术部', manager: '张三', description: '负责公司技术研发' },
    { id: 2, name: '人事部', manager: '李四', description: '负责人力资源管理' },
    { id: 3, name: '财务部', manager: '王五', description: '负责公司财务管理' },
    { id: 4, name: '市场部', manager: '赵六', description: '负责市场营销' }
  ]
}

// 定义mutation
const mutations = {
  // 设置部门列表
  SET_LIST(state, list) {
    state.list = list
  },
  // 添加新部门
  ADD_DEPARTMENT(state, dep) {
    dep.employeeCount = dep.employeeCount || 0
    state.list.push(dep)
  },
  // 编辑部门信息
  EDIT_DEPARTMENT(state, dep) {
    const idx = state.list.findIndex(d => d.id === dep.id)
    if (idx !== -1) state.list.splice(idx, 1, dep)
  },
  // 删除部门
  DELETE_DEPARTMENT(state, id) {
    state.list = state.list.filter(d => d.id !== id)
  },
  // 增加部门员工数量
  INCREMENT_EMPLOYEE_COUNT(state, departmentName) {
    const dept = state.list.find(d => d.name === departmentName)
    if (dept) dept.employeeCount++
  },
  // 减少部门员工数量
  DECREMENT_EMPLOYEE_COUNT(state, departmentName) {
    const dept = state.list.find(d => d.name === departmentName)
    if (dept && dept.employeeCount > 0) dept.employeeCount--
  },
  // 设置部门员工数量
  SET_EMPLOYEE_COUNT(state, { departmentName, count }) {
    const dept = state.list.find(d => d.name === departmentName)
    if (dept) dept.employeeCount = count
  },
  // 更新部门主管
  UPDATE_MANAGER(state, { departmentName, manager }) {
    const dept = state.list.find(d => d.name === departmentName)
    if (dept) dept.manager = manager
  }
}

// 定义action
const actions = {
  // 获取部门列表并更新员工数量
  async fetchList({ commit, state, dispatch, rootState }) {
    // 模拟获取部门数据
    const initialDepartments = [
      { id: 1, name: '技术部', manager: '张三', description: '负责公司技术研发' },
      { id: 2, name: '人事部', manager: '李四', description: '负责人力资源管理' },
      { id: 3, name: '财务部', manager: '王五', description: '负责公司财务管理' },
      { id: 4, name: '市场部', manager: '赵六', description: '负责市场营销' }
    ];

    // 如果状态为空则使用初始数据，否则使用现有状态
    const departmentsToUse = state.list && state.list.length > 0 ? state.list : initialDepartments;

    commit('SET_LIST', departmentsToUse);

    // 获取员工列表以计算数量
    await dispatch('employee/fetchList', null, { root: true });

    // 计算并更新员工数量
    const employeeList = rootState.employee.list;
    const updatedDepartments = departmentsToUse.map(department => {
      const employeeCount = employeeList.filter(employee => employee.department === department.name).length;
      return { ...department, employeeCount };
    });

    commit('SET_LIST', updatedDepartments);
    console.log('department.js: fetched departments and updated employee counts');
  },

  // 添加新部门
  addDepartment({ commit, state }, dep) {
    dep.id = state.list.length ? Math.max(...state.list.map(d => d.id)) + 1 : 1
    commit('ADD_DEPARTMENT', dep)
  },

  // 编辑部门信息
  editDepartment({ commit }, dep) {
    commit('EDIT_DEPARTMENT', dep)
  },

  // 删除部门
  deleteDepartment({ commit }, id) {
    commit('DELETE_DEPARTMENT', id)
  },

  // 更新部门主管
  updateManager({ commit }, { departmentName, manager }) {
    const dept = state.list.find(d => d.name === departmentName)
    if (dept) commit('UPDATE_MANAGER', { departmentName, manager })
  }
}

// 定义getter
const getters = {
  // 获取部门选项列表，用于下拉选择
  getDepartmentOptions: state => {
    return state.list.map(dept => ({
      value: dept.name,
      label: dept.name
    }))
  }
}

// 导出模块
export default {
  namespaced: true, // 启用命名空间
  state,
  mutations,
  actions,
  getters
}